/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator;

import jsf.generator.translator.Translate;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

/**
 *
 * @author abosancic
 */
public class GeneratePropertiesFile {

    public Locale[] allLanguagesUtils =
            //            new Locale[]{Locale.ENGLISH, new Locale("sr", "SERBIAN"), Locale.GERMAN, Locale.FRANCE , Locale.GERMAN, Locale.ITALY};
            new Locale[]{Locale.ENGLISH, new Locale("sr", "SERBIAN")};
    protected List<String> words;
    protected Properties properties;
    protected Locale mainLang;

    public GeneratePropertiesFile() {
    }

    public GeneratePropertiesFile(List<String> words, Locale mainLang) {
        this.words = words;
        this.mainLang = mainLang;
    }

    public GeneratePropertiesFile(String[] words, Locale mainLang) {
        this.words = Arrays.asList(words);
        this.mainLang = mainLang;
    }

    public GeneratePropertiesFile(Properties properties, Locale mainLang) {
        this.properties = properties;
        this.mainLang = mainLang;
    }

    public void generateFile() {

        try {

            for (int i = 0; i < allLanguagesUtils.length; i++) {
                Locale l = allLanguagesUtils[i];
                System.out.println("name: " + l.getLanguage());
                Properties specificLocale = new Properties();

                // skip main lang
                // if data are in properties;
                if (properties != null) {
                    for (String key : properties.stringPropertyNames()) {
                        String value = properties.getProperty(key);
                        //System.out.println(key + " => " + value);
                        if(!mainLang.equals(l))
                        {
                            specificLocale.put(key, Translate.translate(value, mainLang, l));
                        }else{
                            specificLocale = properties;
                            break;
                        }
                    }
                } // or are string
                else {
                    for (int j = 0; j < words.size(); j++) {
                        if(!mainLang.equals(l)){
                            specificLocale.put(words.get(j).replaceAll("\\s+", ""), Translate.translate(words.get(j), mainLang, l));
                        }else{
                            specificLocale.put(words.get(j).replaceAll("\\s+", ""), words.get(j));
                        }
                    }
                }

                // create/write properties file with specific language
               generatePropertiesFile(specificLocale, l);
            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    protected void generatePropertiesFile(Properties properties, Locale locale) {
        JavaPropertiesWriter jpw = new JavaPropertiesWriter();
        jpw.setProperties(properties);
        jpw.setLocale(locale);
        jpw.generateBundle();
    }

    public List<String> getWords() {
        return words;
    }

    public void setWords(List<String> words) {
        this.words = words;
    }
}
